package com.m.crm.service.impl;

import com.m.crm.dao.RankDao;
import com.m.crm.entity.Rank;
import com.m.crm.enums.ResultEnum;
import com.m.crm.service.RankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yugoh on 2017/5/30.
 */
@Service
public class RankServiceImpl implements RankService {
    private final RankDao rankDao;

    @Autowired
    public RankServiceImpl(RankDao rankDao) {
        this.rankDao = rankDao;
    }

    @Transactional
    @Override
    public Rank saveRank(Rank rank) {
        if (rankDao.saveRank(rank) != 1 && rank.getId() == null) {
            throw new RuntimeException(ResultEnum.DB_INSERT_RESULT_ERROR.getMsg());
        }
        return rank;
    }

    @Override
    public List<Rank> listRank(Integer offset, Integer limit) {
        return rankDao.listRank(offset, limit);
    }

    @Override
    public Rank getRankById(Integer rankId) {
        return null;
    }

    @Transactional
    @Override
    public Rank updateRankById(Rank rank) {
        if (rankDao.updateRankbyId(rank) != 1) {
            throw new RuntimeException(ResultEnum.DB_UPDATE_RESULT_ERROR.getMsg());
        }
        return rank;
    }
}
